package smartcounters.androidclient;

import java.util.EnumSet;

import smartcounters.androidclient.WebServices.ConnectionError;
import smartcounters.androidclient.WebServices.InformationUpdaterWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.eUserPermissions;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;

public class CreateUserActivity extends Activity implements OnClickListener, OnResultReceiver{
	private String[] m_permissionsArray;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_user_layout);
        
        Button createUserButton = (Button)findViewById(R.id.buttonCreateUser);
        createUserButton.setOnClickListener(this);
        
		Spinner permissionsSpinner = (Spinner)findViewById(R.id.permissionsSpinner);
        
        m_permissionsArray = new String[2];
        m_permissionsArray[0] = "Administrator";
        m_permissionsArray[1] = "User";
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_permissionsArray);
        permissionsSpinner.setAdapter(adapter);
    }

	public void onClick(View v) {
		if (v.getId() == R.id.buttonCreateUser){
			EditText createUserUsernameEditText = (EditText)findViewById(R.id.createUserUsernameEditText);
			Spinner permissionsSpinner = (Spinner)findViewById(R.id.permissionsSpinner);
			EditText userPhoneEditText = (EditText)findViewById(R.id.userPhoneEditText);
			EditText userMobilePhoneEditText = (EditText)findViewById(R.id.userMobilePhoneEditText);
			EditText emailEditText = (EditText)findViewById(R.id.emailEditText);
			EditText userPasswordEditText = (EditText)findViewById(R.id.userPasswordEditText);
			EditText accountsAssociatedWithEditText = (EditText)findViewById(R.id.accountsAssociatedWithEditText);
			Button createUserButton = (Button)findViewById(R.id.buttonCreateUser);
			
			int[] accounts = null;
			String[] accountsStrings = accountsAssociatedWithEditText.getText().toString().split(",");
			if (accountsStrings.length == 1 && accountsStrings[0].length() == 0)
			{
				accounts = new int[0];
			}
			else
			{
				accounts = new int[accountsStrings.length];
				for (int i = 0; i < accountsStrings.length; i++){
					accounts[i] = Integer.parseInt(accountsStrings[i]);
				}
			}
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			EnumSet<eUserPermissions> permissions;
			
			if (((String)permissionsSpinner.getSelectedItem()) == "Administrator"){
				permissions = EnumSet.of(eUserPermissions.All);
			}
			else{
				permissions = EnumSet.of(
						eUserPermissions.CreateFault,
						eUserPermissions.EditAssociatedAccountInfo,
						eUserPermissions.EditOwnUserInfo,
						eUserPermissions.ReadAssociatedAccountInfo,
						eUserPermissions.ReadOwnBill,
						eUserPermissions.ReadOwnUserInfo,
						eUserPermissions.UpdateUsage);
			}
			
			createUserUsernameEditText.setEnabled(false);
			permissionsSpinner.setEnabled(false);
			userPhoneEditText.setEnabled(false);
			userMobilePhoneEditText.setEnabled(false);
			emailEditText.setEnabled(false);
			userPasswordEditText.setEnabled(false);
			accountsAssociatedWithEditText.setEnabled(false);
			createUserButton.setEnabled(false);
			
			updaterClient.RegisterResultReceiver(this);
			
			TextView messageTextView = (TextView)findViewById(R.id.userCreateMessageTextView);
			messageTextView.setText("Please wait...");
			
			updaterClient.CreateUser(
					Configuration.getInstance().getUserCredentials(),
					createUserUsernameEditText.getText().toString(),
					permissions,
					userPhoneEditText.getText().toString(),
					userMobilePhoneEditText.getText().toString(),
					emailEditText.getText().toString(),
					userPasswordEditText.getText().toString(),
					accounts);
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText createUserUsernameEditText = (EditText)findViewById(R.id.createUserUsernameEditText);
		Spinner permissionsSpinner = (Spinner)findViewById(R.id.permissionsSpinner);
		EditText userPhoneEditText = (EditText)findViewById(R.id.userPhoneEditText);
		EditText userMobilePhoneEditText = (EditText)findViewById(R.id.userMobilePhoneEditText);
		EditText emailEditText = (EditText)findViewById(R.id.emailEditText);
		EditText userPasswordEditText = (EditText)findViewById(R.id.userPasswordEditText);
		EditText accountsAssociatedWithEditText = (EditText)findViewById(R.id.accountsAssociatedWithEditText);
		Button createUserButton = (Button)findViewById(R.id.buttonCreateUser);
		
		if (!(i_Msg.obj instanceof ConnectionError)){
			TextView messageTextView = (TextView)findViewById(R.id.userCreateMessageTextView);
			messageTextView.setText("The requested user was created.");
			
			createUserUsernameEditText.setText("");
			permissionsSpinner.setSelection(0);
			userPhoneEditText.setText("");
			userMobilePhoneEditText.setText("");
			emailEditText.setText("");
			userPasswordEditText.setText("");
			accountsAssociatedWithEditText.setText("");
		}
		else{
			TextView messageTextView = (TextView)findViewById(R.id.userCreateMessageTextView);
			messageTextView.setText("An error occured.");
		}
		
		createUserUsernameEditText.setEnabled(true);
		permissionsSpinner.setEnabled(true);
		userPhoneEditText.setEnabled(true);
		userMobilePhoneEditText.setEnabled(true);
		emailEditText.setEnabled(true);
		userPasswordEditText.setEnabled(true);
		accountsAssociatedWithEditText.setEnabled(true);
		createUserButton.setEnabled(true);
	}
}